package org.springframework.core.convert.converter;

import java.util.Objects;
import java.util.Set;

/**
 * @Name GenericConverter
 * @Description: 类型转换 [通用] 接口
 * @Version v1.0.0
 * @Author: lichao
 * @Date: 2024-04-11 15:03:48
 **/
public interface GenericConverter {

    /**
     * 获取转换类型对集合
     *
     * @return java.util.Set<org.springframework.core.convert.converter.GenericConverter.ConvertiblePair> 结果
     * @version v1.0.0
     * @author lichao
     * @date 2024-04-11 15:11:01
     */
    Set<ConvertiblePair> getConvertibleTypes();

    /**
     * 将源类型 [sourceType] 转换成目标类型 [targetType]
     *
     * @param source     源类型对象
     * @param sourceType 源类型
     * @param targetType 目标类型
     * @return java.lang.Object 目标对象
     * @version v1.0.0
     * @author lichao
     * @date 2024-04-11 15:11:46
     */
    Object convert(Object source, Class sourceType, Class targetType);

    public static final class ConvertiblePair {
        private final Class<?> sourceType;
        private final Class<?> targetType;

        public ConvertiblePair(Class<?> sourceType, Class<?> targetType) {
            this.sourceType = sourceType;
            this.targetType = targetType;
        }

        public Class<?> getSourceType() {
            return sourceType;
        }

        public Class<?> getTargetType() {
            return targetType;
        }

        @Override
        public boolean equals(Object object) {
            if (this == object) return true;
            if (object == null || object.getClass() != ConvertiblePair.class) return false;
            ConvertiblePair other = (ConvertiblePair) object;
            return sourceType.equals(other.sourceType) && targetType.equals(other.targetType);
        }

        @Override
        public int hashCode() {
            return sourceType.hashCode() * 31 + targetType.hashCode();
        }
    }

}
